home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zgeneric.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  12KB  |  466 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zgeneric.c */
  20. /* Array/string/dictionary generic operators for PostScript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "dict.h"
  26. #include "estack.h"    /* for forall */
  27. #include "iname.h"
  28. #include "ivmspace.h"
  29. #include "packed.h"
  30. #include "store.h"
  31.  
  32. /* This file implements copy, get, put, getinterval, putinterval, */
  33. /* length, and forall, which apply generically to */
  34. /* arrays, strings, and dictionaries.  (Copy also has a special */
  35. /* meaning for copying the top N elements of the stack.) */
  36.  
  37. /* See the comment in opdef.h for an invariant which allows */
  38. /* more efficient implementation of forall. */
  39.  
  40. /* Imported operators */
  41. extern int zcopy_dict(P1(os_ptr));
  42.  
  43. /* Forward references */
  44. private int zcopy_invalid(P1(os_ptr));
  45. private int zcopy_integer(P1(os_ptr));
  46. private int zcopy_interval(P1(os_ptr));
  47. private int copy_interval(P4(os_ptr, uint, os_ptr, const char *));
  48.  
  49. /* The type dispatch table for `copy'. */
  50. /* We export it so that Level 2 can extend it to handle gstates. */
  51. op_proc_p zcopy_procs[t_next_index];
  52.  
  53. /* Initialize the generic dispatch table. */
  54. private void
  55. zgeneric_init(void)
  56. {    int i;
  57.     for ( i = 0; i < t_next_index; i++ )
  58.         zcopy_procs[i] = zcopy_invalid;
  59.     zcopy_procs[t_integer] = zcopy_integer;
  60.     zcopy_procs[t_array] = zcopy_procs[t_string] = zcopy_interval;
  61.     zcopy_procs[t_dictionary] = zcopy_dict;
  62. }
  63.  
  64. /* <various1> <various2> copy <various> */
  65. /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
  66. /* Note that this implements copy for arrays and strings, */
  67. /* but not for dictionaries (see zcopy_dict in zdict.c). */
  68. int
  69. zcopy(register os_ptr op)
  70. {    int type = r_type(op);
  71.     if ( type >= t_next_index )
  72.         return_error(e_typecheck);
  73.     return (*zcopy_procs[type])(op);
  74. }
  75. /* <other> copy */
  76. private int
  77. zcopy_invalid(os_ptr op)
  78. {    return_error(e_typecheck);
  79. }
  80. /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
  81. private int
  82. zcopy_integer(register os_ptr op)
  83. {    os_ptr op1 = op - 1;
  84.     int count;
  85.     if ( (ulong)op->value.intval > op - osbot )
  86.         return_error(e_rangecheck);
  87.     count = op->value.intval;
  88.     if ( op1 + count > ostop )
  89.         return_error(e_stackoverflow);
  90.     memcpy((char *)op, (char *)(op - count), count * sizeof(ref));
  91.     push(count - 1);
  92.     return 0;
  93. }
  94. /* <array1> <array2> copy <subarray2> */
  95. /* <string1> <string2> copy <substring2> */
  96. private int
  97. zcopy_interval(register os_ptr op)
  98. {    os_ptr op1 = op - 1;
  99.     int code = copy_interval(op, 0, op1, "copy");
  100.     if ( code < 0 ) return code;
  101.     r_set_size(op, r_size(op1));
  102.     *op1 = *op;
  103.     pop(1);
  104.     return 0;
  105. }
  106.  
  107. /* <array|dict|name|packedarray|string> length <int> */
  108. int
  109. zlength(register os_ptr op)
  110. {    switch ( r_type(op) )
  111.        {
  112.     case t_array:
  113.     case t_string:
  114.     case t_mixedarray:
  115.     case t_shortarray:
  116.         check_read(*op);
  117.         make_int(op, r_size(op));
  118.         return 0;
  119.     case t_dictionary:
  120.         check_dict_read(*op);
  121.         make_int(op, dict_length(op));
  122.         return 0;
  123.     case t_name:
  124.        {    ref str;
  125.         name_string_ref(op, &str);
  126.         make_int(op, r_size(&str));
  127.        }
  128.         return 0;
  129.     default:
  130.         return_error(e_typecheck);
  131.        }
  132. }
  133.  
  134. /* <array|packedarray|string> <index> get <obj> */
  135. /* <dict> <key> get <obj> */
  136. int
  137. zget(register os_ptr op)
  138. {    os_ptr op1 = op - 1;
  139.     ref *pvalue;
  140.     switch ( r_type(op1) )
  141.        {
  142.     case t_dictionary:
  143.         check_dict_read(*op1);
  144.         if ( dict_find(op1, op, &pvalue) <= 0 )
  145.             return_error(e_undefined);
  146.         op[-1] = *pvalue;
  147.         break;
  148.     case t_string:
  149.         check_type(*op, t_integer);
  150.         check_read(*op1);
  151.         if ( (ulong)(op->value.intval) >= r_size(op1) )
  152.             return_error(e_rangecheck);
  153.         make_int(op1, op1->value.bytes[(uint)op->value.intval]);
  154.         break;
  155.     default:
  156.        {    int code;
  157.         check_type(*op, t_integer);
  158.         check_read(*op1);
  159.         code = array_get(op1, op->value.intval, op1);
  160.         if ( code < 0 ) return code;
  161.        }
  162.        }
  163.     pop(1);
  164.     return 0;
  165. }
  166.  
  167. /* <array> <index> <obj> put - */
  168. /* <dict> <key> <value> put - */
  169. /* <string> <index> <int> put - */
  170. int
  171. zput(register os_ptr op)
  172. {    os_ptr op1 = op - 1;
  173.     os_ptr op2 = op1 - 1;
  174.     switch ( r_type(op2) )
  175.        {
  176.     case t_dictionary:
  177.         check_dict_write(*op2);
  178.        {    int code = dict_put(op2, op1, op);
  179.         if ( code ) return code;    /* error */
  180.        }
  181.         break;
  182.     case t_array:
  183.         check_type(*op1, t_integer);
  184.         check_write(*op2);
  185.         if ( (ulong)(op1->value.intval) >= r_size(op2) )
  186.             return_error(e_rangecheck);
  187.         if ( !r_is_global(op) && r_is_global(op2) )
  188.             return_error(e_invalidaccess);
  189.        {    ref *eltp = op2->value.refs + (uint)op1->value.intval;
  190.         ref_assign_old(eltp, op, "put");
  191.        }    break;
  192.     case t_mixedarray:        /* packed arrays are read-only */
  193.     case t_shortarray:
  194.         return_error(e_invalidaccess);
  195.     case t_string:
  196.         check_type(*op1, t_integer);
  197.         check_write(*op2);
  198.         if ( (ulong)(op1->value.intval) >= r_size(op2) )
  199.             return_error(e_rangecheck);
  200.         check_type(*op, t_integer);
  201.         if ( (ulong)op->value.intval > 0xff )
  202.             return_error(e_rangecheck);
  203.         op2->value.bytes[(uint)op1->value.intval] = (byte)op->value.intval;
  204.         break;
  205.     default:
  206.         return_error(e_typecheck);
  207.        }
  208.     pop(3);
  209.     return 0;
  210. }
  211.  
  212. /* <seq:array|packedarray|string> <index> <count> getinterval <subseq> */
  213. int
  214. zgetinterval(register os_ptr op)
  215. {    os_ptr op1 = op - 1;
  216.     os_ptr op2 = op1 - 1;
  217.     uint index;
  218.     uint count;
  219.     check_type(*op1, t_integer);
  220.     check_type(*op, t_integer);
  221.     switch ( r_type(op2) )
  222.        {
  223.     default:
  224.         return_error(e_typecheck);
  225.     case t_array: case t_string:
  226.     case t_mixedarray:
  227.     case t_shortarray: ;
  228.        }
  229.     check_read(*op2);
  230.     if ( (ulong)op1->value.intval > r_size(op2) )
  231.         return_error(e_rangecheck);
  232.     index = op1->value.intval;
  233.     if ( (ulong)op->value.intval > r_size(op2) - index )
  234.         return_error(e_rangecheck);
  235.     count = op->value.intval;
  236.     switch ( r_type(op2) )
  237.        {
  238.     case t_array: op2->value.refs += index; break;
  239.     case t_string: op2->value.bytes += index; break;
  240.     case t_mixedarray:
  241.        {    const ref_packed *packed = op2->value.packed;
  242.         for ( ; index--; ) packed = packed_next(packed);
  243.         op2->value.packed = packed;
  244.        }    break;
  245.     case t_shortarray: op2->value.packed += index; break;
  246.        }
  247.     r_set_size(op2, count);
  248.     pop(2);
  249.     return 0;
  250. }
  251.  
  252. /* <array1> <index> <array2|packedarray2> putinterval - */
  253. /* <string1> <index> <string2> putinterval - */
  254. int
  255. zputinterval(register os_ptr op)
  256. {    os_ptr opindex = op - 1;
  257.     os_ptr opto = opindex - 1;
  258.     int code;
  259.     check_type(*opindex, t_integer);
  260.     switch ( r_type(opto) )
  261.        {
  262.     default:
  263.         return_error(e_typecheck);
  264.     case t_mixedarray: case t_shortarray:
  265.         return_error(e_invalidaccess);
  266.     case t_array: case t_string: ;
  267.        }
  268.     check_write(*opto);
  269.     if ( (ulong)opindex->value.intval > r_size(opto) )
  270.         return_error(e_rangecheck);
  271.     code = copy_interval(opto, (uint)(opindex->value.intval), op, "putinterval");
  272.     if ( code >= 0 ) pop(3);
  273.     return code;
  274. }
  275.  
  276. /* <array|packedarray|string> <<element> proc> forall - */
  277. /* <dict> <<key> <value> proc> forall - */
  278. private int
  279.   array_continue(P1(os_ptr)),
  280.   dict_continue(P1(os_ptr)),
  281.   string_continue(P1(os_ptr)),
  282.   packedarray_continue(P1(os_ptr));
  283. private int forall_cleanup(P1(os_ptr));
  284. int
  285. zforall(register os_ptr op)
  286. {    os_ptr obj = op - 1;
  287.     register es_ptr ep = esp;
  288.     uint index = 0;            /* only used for dictionaries */
  289.     check_estack(6);
  290. #define cproc (ep + 5)
  291.     switch ( r_type(obj) )
  292.        {
  293.     default:
  294.         return_error(e_typecheck);
  295.     case t_array:
  296.         check_read(*obj);
  297.         make_op_estack(cproc, array_continue);
  298.         break;
  299.     case t_dictionary:
  300.         check_dict_read(*obj);
  301.         make_op_estack(cproc, dict_continue);
  302.         index = dict_first(obj);
  303.         break;
  304.     case t_string:
  305.         check_read(*obj);
  306.         make_op_estack(cproc, string_continue);
  307.         break;
  308.     case t_mixedarray:
  309.     case t_shortarray:
  310.         check_read(*obj);
  311.         make_op_estack(cproc, packedarray_continue);
  312.         break;
  313.        }
  314.     check_proc(*op);
  315.     /* Push a mark, the composite object, the iteration index, */
  316.     /* and the procedure, and invoke the continuation operator. */
  317.     make_mark_estack(ep + 1, es_for, forall_cleanup);
  318.     ep[2] = *obj;
  319.     make_int(ep + 3, index);
  320.     ep[4] = *op;
  321.     esp += 4;
  322.     pop(2);  op -= 2;
  323.     return (*real_opproc(cproc))(op);
  324. #undef cproc
  325. }
  326. /* Continuation operator for arrays */
  327. private int
  328. array_continue(register os_ptr op)
  329. {    es_ptr obj = esp - 2;
  330.     if ( r_size(obj) )        /* continue */
  331.        {    push(1);
  332.         r_inc_size(obj, -1);
  333.         *op = *obj->value.refs;
  334.         obj->value.refs++;
  335.         esp += 2;
  336.         *esp = obj[2];
  337.         return o_push_estack;
  338.        }
  339.     else                /* done */
  340.        {    esp -= 4;        /* pop mark, object, index, proc */
  341.         return o_pop_estack;
  342.        }
  343. }
  344. /* Continuation operator for dictionaries */
  345. private int
  346. dict_continue(register os_ptr op)
  347. {    es_ptr obj = esp - 2;
  348.     int index = (int)esp[-1].value.intval;
  349.     push(2);            /* make room for key and value */
  350.     if ( (index = dict_next(obj, index, op - 1)) >= 0 )    /* continue */
  351.        {    esp[-1].value.intval = index;
  352.         esp += 2;
  353.         *esp = obj[2];
  354.         return o_push_estack;
  355.        }
  356.     else                /* done */
  357.        {    pop(2);            /* undo push */
  358.         esp -= 4;        /* pop mark, object, index, proc */
  359.         return o_pop_estack;
  360.        }
  361. }
  362. /* Continuation operator for strings */
  363. private int
  364. string_continue(register os_ptr op)
  365. {    es_ptr obj = esp - 2;
  366.     if ( r_size(obj) )        /* continue */
  367.        {    r_inc_size(obj, -1);
  368.         push(1);
  369.         make_int(op, *obj->value.bytes);
  370.         obj->value.bytes++;
  371.         esp += 2;
  372.         *esp = obj[2];
  373.         return o_push_estack;
  374.        }
  375.     else                /* done */
  376.        {    esp -= 4;        /* pop mark, object, index, proc */
  377.         return o_pop_estack;
  378.        }
  379. }
  380. /* Continuation operator for packed arrays */
  381. private int
  382. packedarray_continue(register os_ptr op)
  383. {    es_ptr obj = esp - 2;
  384.     if ( r_size(obj) )        /* continue */
  385.        {    const ref_packed *packed = obj->value.packed;
  386.         r_inc_size(obj, -1);
  387.         push(1);
  388.         packed_get(packed, op);
  389.         obj->value.packed = packed_next(packed);
  390.         esp += 2;
  391.         *esp = obj[2];
  392.         return o_push_estack;
  393.        }
  394.     else                /* done */
  395.        {    esp -= 4;        /* pop mark, object, index, proc */
  396.         return o_pop_estack;
  397.        }
  398. }
  399. /* Vacuous cleanup procedure */
  400. private int
  401. forall_cleanup(os_ptr op)
  402. {    return 0;
  403. }
  404.  
  405. /* ------ Initialization procedure ------ */
  406.  
  407. op_def zgeneric_op_defs[] = {
  408.     {"1copy", zcopy},
  409.     {"2forall", zforall},
  410.     {"2get", zget},
  411.     {"3getinterval", zgetinterval},
  412.     {"1length", zlength},
  413.     {"3put", zput},
  414.     {"3putinterval", zputinterval},
  415.         /* Internal operators */
  416.     {"0%array_continue", array_continue},
  417.     {"0%dict_continue", dict_continue},
  418.     {"0%packedarray_continue", packedarray_continue},
  419.     {"0%string_continue", string_continue},
  420.     op_def_end(zgeneric_init)
  421. };
  422.  
  423. /* ------ Shared routines ------ */
  424.  
  425. /* Copy an interval from one operand to another. */
  426. /* This is used by both putinterval and string/array copy. */
  427. /* One operand is known to be an array or string, */
  428. /* and the starting index is known to be less than or equal to */
  429. /* its length; nothing else has been checked. */
  430. private int
  431. copy_interval(os_ptr prto, uint index, os_ptr prfrom, const char *cname)
  432. {    int fromtype = r_type(prfrom);
  433.     uint fromsize = r_size(prfrom);
  434.     if ( !(fromtype == r_type(prto) || (fromtype == t_shortarray
  435.         || fromtype == t_mixedarray) && r_type(prto) == t_array)
  436.        )
  437.         return_error(e_typecheck);
  438.     check_read(*prfrom);
  439.     check_write(*prto);
  440.     if ( fromsize > r_size(prto) - index )
  441.         return_error(e_rangecheck);
  442.     switch ( fromtype )
  443.        {
  444.     case t_array:
  445.         return refcpy_to_old(prto, index, prfrom->value.refs,
  446.                      fromsize, cname);
  447.     case t_string:
  448.         memcpy(prto->value.bytes + index, prfrom->value.bytes,
  449.                fromsize);
  450.         break;
  451.     case t_mixedarray:
  452.     case t_shortarray:
  453.        {    int i;
  454.         const ref_packed *packed = prfrom->value.packed;
  455.         ref *pdest = prto->value.refs + index;
  456.         ref elt;
  457.         for ( i = 0; i < fromsize; i++, pdest++ )
  458.          { packed_get(packed, &elt);
  459.            ref_assign_old(pdest, &elt, cname);
  460.            packed = packed_next(packed);
  461.          }
  462.        }    break;
  463.        }
  464.     return 0;
  465. }
  466.